/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments;


import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.crayon.account.dao.DAOAccount;
import com.crayon.account.data.EDAType;
import com.crayon.account.data.ENMapKey;
import com.crayon.account.data.ItemGroup;
import com.crayon.account.impl.OnDialogFragment;
import com.crayon.account.item.adapter.AdapterAccountExpandable;
import com.crayon.sharemenu.R;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FragmentChildExListCate extends FragmentChildExList 
implements 
OnClickListener
,OnChildClickListener
,OnGroupClickListener
,OnDialogFragment
{

	public static FragmentChildExListCate onCrateInstance(Date time){
		FragmentChildExListCate f = new FragmentChildExListCate();
		Bundle args =new Bundle();
		args.putLong(PARAM_DATE, time.getTime());
		f.setArguments(args);
		return f;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onLoadFinished(Loader<HashMap<ENMapKey, Object>> arg0,
			HashMap<ENMapKey, Object> arg1) {
		// TODO Auto-generated method stub
		super.onLoadFinished(arg0, arg1);
		hashMap.clear();
		groupDatas.clear();
		childData.clear();
		List<DAOAccount> daoAccounts = (List)arg1.get(ENMapKey.DAOLISTARRAY);
		for(DAOAccount item : daoAccounts){
			
			String key = !StringUtils.isEmpty(item.getxCategory())? item.getxCategory():getString(R.string.dao_key_nocate);

			LinkedList<DAOAccount> list =  new LinkedList<DAOAccount>();
			if(hashMap.containsKey(key)) {
				list =	hashMap.get(key);
			}else{
				hashMap.put(key, list);
			}
			list.add(item);
		}
		
		List<String> sortedKeys = new ArrayList(hashMap.keySet());
		Collections.sort(sortedKeys, Collections.reverseOrder()); //Collections.sort(sortedKeys);  //Collections.reverse(sortedKeys);
		for (String key : sortedKeys) {
			ItemGroup itemGroup =new ItemGroup();
			itemGroup.setKey(key);
			
			List<Map<String,DAOAccount>> child =new ArrayList<Map<String,DAOAccount>>();
			
			int childCount=0;
			for(DAOAccount item: hashMap.get(key)){
				Map<String, DAOAccount> map = new HashMap<String, DAOAccount>(); 
				map.put(key, item);
				String type = item.getSummary();
				child.add(map);

				itemGroup.setChildCount(++childCount);
				if(item.getxIOType()==EDAType.INCOM.getKey()){
					itemGroup.setPriceIncom(item.getXprice() +itemGroup.getPriceIncom());
				}else if(item.getxIOType()==EDAType.OUTCOM.getKey()){
					itemGroup.setPriceOutcom(item.getXprice() + itemGroup.getPriceOutcom());
				}
				itemGroup.setPriceTotal(item.getXprice() + itemGroup.getPriceTotal());
			}

			HashMap<String,ItemGroup> groupItem =new HashMap<String, ItemGroup>();
			groupItem.put(AdapterAccountExpandable.NAME_GROUP,itemGroup );
			childData.add(child);
			groupDatas.add(groupItem);

		}
		ExpandableListView expandableListView = (ExpandableListView)getView().findViewById(R.id.listExpandableListView);
		if(mAdapterExpandable.getGroupCount() > 0){
			for(int groupPos =0; groupPos < mAdapterExpandable.getGroupCount(); groupPos++){
				expandableListView.expandGroup(groupPos);
			}
		}
		mAdapterExpandable.notifyDataSetChanged();
		
	}

}
